﻿package  {
	import flash.display.MovieClip;
	
	import flash.events.MouseEvent;
	
	public class GuessPanel extends MovieClip{
		
		private var used_arr:Array;
		private var feedback_arr:Array;
		private var wait4_arr:Array;
		
		private var pos_arr:Array;
		
		public function GuessPanel() {
			init();
			
			restart_btn.addEventListener(MouseEvent.CLICK, restartHandler);
			submit_btn.addEventListener(MouseEvent.CLICK, submitHandler);
		}

		private function init(){
			guess_txt.text = "";
			used_arr = new Array();
			feedback_arr = new Array();
			wait4_arr = new Array();
			hint_txt.text = "";
			
			showHint();
			
			for(var i:uint = 0; i < 10; i ++){
				this["wait_txt" + i].text = "";
				this["used_txt" + i].text = "";
				this["used_txt" + i].addEventListener(MouseEvent.CLICK, txtClickHandler);
			}
			
			pos_arr = new Array(4);
			for(var j:uint = 0; j < 4; j ++)
				pos_arr[j] = [0,1,2,3,4,5,6,7,8,9];
			
			guess();
		}
		
		private function calculation(input:String, feedbackStr:String):Boolean{
			if(input.length < 4 && feedbackStr.length < 2) return false;
			
			var num_arr:Array = new Array(4);
			for(var ni:uint = 0; ni < 4; ni ++){
				num_arr[ni] = uint(input.substr(ni, 1));
			}
			
			var sign_arr:Array = new Array(2);
			for(var si:uint = 0; si < 2; si ++){
				sign_arr[si] = uint(feedbackStr.substr(si, 1));
			}
			
			if(sign_arr[0] == 0 && sign_arr[1] == 0){
				removeFun(true, true, 0, num_arr);
			}
			else if(sign_arr[0] == 0 && sign_arr[1] > 0){
				removeFun(true, false, 0, num_arr);
			}
			else if(sign_arr[0] > 0 && sign_arr[1] == 0){
				removeFun(false, true, 0, num_arr);
			}
			if(sign_arr[0] + sign_arr[1] == 4){
				for(var i:uint = 0; i < 4; i ++){
					var tmpI:uint = pos_arr[i].length;
					while(tmpI > 0){
						if(input.indexOf(pos_arr[i][tmpI - 1]) == -1){
							pos_arr[i].splice(tmpI - 1, 1);
						}
						tmpI --;
					}
				}
			}
			
			for(var loopI:uint = 0; loopI < used_arr.length - 1; loopI ++){
				var lastStr = used_arr[loopI];
				var last_num_arr:Array = new Array(4);
				for(var lni:uint = 0; lni < 4; lni ++){
					last_num_arr[lni] = uint(lastStr.substr(lni, 1));
				}
				
				var lastFeedback = feedback_arr[loopI];
				var last_sign_arr:Array = new Array(2);
				for(var lsi:uint = 0; lsi < 2; lsi ++){
					last_sign_arr[lsi] = uint(lastFeedback.substr(lsi, 1));
				}
				
				for(var lli:uint = 4; lli > 0; lli --){
					if(last_num_arr[lli % 4] == num_arr[lli % 4] && last_num_arr[(lli + 1) % 4] == num_arr[(lli + 1) % 4] 
						  && last_num_arr[(lli + 2) % 4] == num_arr[(lli + 2) % 4]){
						if(last_sign_arr[0] == sign_arr[0]){
							if(last_sign_arr[1] < sign_arr[1]){//非当前数字为非组合数
								removeFun(true, true, lli, last_num_arr);
								if(wait4_arr.indexOf(num_arr[lli - 1]) == -1)wait4_arr.push(num_arr[lli - 1]);
							}
							else if(last_sign_arr[1] > sign_arr[1]){
								removeFun(true, true, lli, num_arr);
								if(wait4_arr.indexOf(last_num_arr[lli - 1]) == -1)wait4_arr.push(last_num_arr[lli - 1]);
							}
						}
						else if(last_sign_arr[1] == sign_arr[1]){
							if(last_sign_arr[0] < sign_arr[0]){//非当前数字为非组合数 且当前数字非当前位为非组合数
								removeFun(true, true, lli, last_num_arr);
								pos_arr[lli - 1] = [num_arr[lli - 1]];
							}
							else if(last_sign_arr[0] > sign_arr[0]){
								removeFun(true, true, lli, num_arr);
								pos_arr[lli - 1] = [last_num_arr[lli - 1]];
							}
						}
					}
				}
			}
			
			if(wait4_arr.length == 4){
				for(var ci:uint = 0; ci < 4; ci ++){
					tmpI = pos_arr[ci].length;
					while(tmpI > 0){
						if(wait4_arr.indexOf(pos_arr[ci][tmpI - 1]) == -1){
							pos_arr[ci].splice(tmpI - 1, 1);
						}
						tmpI --;
					}
				}
			}
			
			showWait();
			
			return true;
		}
		
		private function removeFun(removeSelf:Boolean, removeOthers:Boolean, removeNumIndex:uint, removeArr:Array){
			if(removeSelf){
				if(removeNumIndex == 0){
					for(var rsi:uint = 0; rsi < 4; rsi ++){
						var tsi:int = pos_arr[rsi].indexOf(removeArr[rsi]);
						if(tsi >= 0)pos_arr[rsi].splice(tsi, 1);
					}
				}
				else{
					var ti:int = pos_arr[removeNumIndex - 1].indexOf(removeArr[removeNumIndex - 1]);
					if(ti >= 0)pos_arr[removeNumIndex - 1].splice(ti, 1);
				}
			}
			if(removeOthers){
				if(removeNumIndex == 0){
					for(var roi:uint = 0; roi < 4; roi ++){
						for(var ri:uint = 0; ri < 4; ri ++){
							if(ri == roi)continue;
							var toi = pos_arr[roi].indexOf(removeArr[ri]);
							if(toi >= 0)pos_arr[roi].splice(toi, 1);
						}						
					}
				}
				else{
					for(var rpi:uint = 0; rpi < 4; rpi ++){
						if(rpi == removeNumIndex - 1)continue;
						var tti = pos_arr[rpi].indexOf(removeArr[removeNumIndex - 1]);
						if(tti >= 0)pos_arr[rpi].splice(tti, 1);
					}
				}
			}
		}
		
		private function showWait(){
			for(var i:uint = 0; i < 10; i ++){
				this["wait_txt" + i].text = "";
			}
			
			var count:uint = 0;
			for(var i1:uint = 0; i1 < pos_arr[0].length; i1 ++){
				for(var i2:uint = 0; i2 < pos_arr[1].length; i2 ++){
					if(pos_arr[1][i2] == pos_arr[0][i1]) continue;
					for(var i3:uint = 0; i3 < pos_arr[2].length; i3 ++){
						if(pos_arr[2][i3] == pos_arr[0][i1] || pos_arr[2][i3] == pos_arr[1][i2]) continue;
						for(var i4:uint = 0; i4 < pos_arr[3].length; i4 ++){
							if(pos_arr[3][i4] == pos_arr[0][i1] || pos_arr[3][i4] == pos_arr[1][i2] || pos_arr[3][i4] == pos_arr[2][i3]) continue;
							var tmpFlag:Boolean = true;
							for(var usedI:uint; usedI < used_arr.length; usedI ++)
								if(used_arr[usedI] == String(pos_arr[0][i1]) + String(pos_arr[1][i2]) + String(pos_arr[2][i3]) + String(pos_arr[3][i4])){ 
									tmpFlag = false;
									break;
								}
							if(tmpFlag){
								this["wait_txt" + count].text =  String(pos_arr[0][i1]) + String(pos_arr[1][i2]) + String(pos_arr[2][i3]) + String(pos_arr[3][i4]);
								count ++;
								if(count == 10) return;
							}
						}						
					}					
				}
			}
		}
		
		private function restartHandler(e:MouseEvent){
			init();
		}
		
		private function submitHandler(e:MouseEvent){
			used_arr.push(guess_txt.text);
			feedback_arr.push(input_txt.text);
			
			if(calculation(guess_txt.text, input_txt.text)){
				for(var ui:uint = 0; ui < 10; ui ++){
					var tti:uint = used_arr.length - ui;
					this["used_txt" + ui].text = tti + "  " + used_arr[tti - 1] + "    " + feedback_arr[tti - 1];
					if(ui == used_arr.length - 1) break;
				}
				guess();
			}
		}
		
		private function guess(){
			if(used_arr.length == 0) guess_txt.text = "1234";
			else if(used_arr.length == 1) guess_txt.text = "4567";
			else if(used_arr.length == 2) guess_txt.text = "7890";
			else guess_txt.text = wait_txt0.text;
		}
		
		private function txtClickHandler(e:MouseEvent){
			var txtIndex:uint = uint(e.target.name.substr(8, 1));
			var realIndex:uint = used_arr.length - txtIndex;
			
			guess_txt.text = used_arr[realIndex - 1];
			input_txt.text = feedback_arr[realIndex - 1];
			
			used_arr.splice(realIndex - 1, txtIndex + 1);
			feedback_arr.splice(realIndex - 1, txtIndex + 1);
			wait4_arr = new Array();
			
			for(var i:uint = 0; i < 4; i ++){
				pos_arr[i] = [0,1,2,3,4,5,6,7,8,9];
			}
			
			for(var ui:uint = 0; ui < 10; ui ++){
				var tti:uint = used_arr.length - ui;
				this["used_txt" + ui].text = tti + "  " + used_arr[tti - 1] + "    " + feedback_arr[tti - 1];
				if(ui > used_arr.length - 1) this["used_txt" + ui].text = "";
			}
			
			for(var txtI:uint = 0; txtI < realIndex - 1; txtI ++){
				calculation(used_arr[txtI], feedback_arr[txtI]);
			}
		}
		
		private function showHint(){
			var tmpNum_arr = [0,1,2,3,4,5,6,7,8,9];
			
			for(var tmpI:uint = 0; tmpI < 4; tmpI ++){
				var rdmIndex:uint = uint(Math.random() * tmpNum_arr.length);
				var rdmArr:Array = tmpNum_arr.splice(rdmIndex, 1);
				hint_txt.appendText(String(rdmArr[0]));
			}
		}
	}
	
}
